package main

import (
	"fmt"
	"math"
	"time"
)

const c string = "你好"

//TIP <p>To run your code, right-click the code and select <b>Run</b>.</p> <p>Alternatively, click
// the <icon src="AllIcons.Actions.Execute"/> icon in the gutter and select the <b>Run</b> menu item from here.</p>

func main() {

	fmt.Println(c)
	//TIP <p>Press <shortcut actionId="ShowIntentionActions"/> when your caret is at the underlined text
	// to see how GoLand suggests fixing the warning.</p><p>Alternatively, if available, click the lightbulb to view possible fixes.</p>
	s := "gopher"
	fmt.Printf("Hello and welcome, %s!\n", s)
	const f1 = "你好么"
	fmt.Println(f1)
	for i := 1; i <= 5; i++ {
		//TIP <p>To start your debugging session, right-click your code in the editor and select the Debug option.</p> <p>We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
		// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.</p>
		fmt.Println("i =", 100/i)
	}
	fmt.Println("Done!"+"go"+"long", 7/3)
	var name = "hello"
	fmt.Println(name)
	var a, b int = 1, 2
	fmt.Println(a, b)
	var nam, address string = "hello", "天安门"
	fmt.Println(nam, address)
	var e int
	fmt.Println(e)
	f := "apple"
	fmt.Println(f)
	const n = 500000000
	fmt.Println(math.Sin(n))
	const d = 3e20 / n
	fmt.Println(d)
	fmt.Println(int64(d))
	i := 1
	for i <= 3 {
		fmt.Println(i)
		i++
	}
	for j := 0; j < 10; j++ {
		fmt.Println(j)
	}
	for {
		fmt.Println("loop")
		break
	}
	for n := range 6 {
		if n%2 == 0 && n != 0 {
			fmt.Println(n)
			continue
		}
	}
	j := 3
	fmt.Print("write ", j, " as ")
	switch j {
	case 1:
		fmt.Println("one")
	case 2:
		fmt.Println("two")
	case 3:
		fmt.Println("three")
	default:
		fmt.Println("unknown")
	}
	switch time.Now().Weekday() {
	case time.Saturday, time.Sunday:
		fmt.Println("It's the weekend")
	default:
		fmt.Println("It's a weekday")
	}

	t := time.Now()
	switch {
	case t.Hour() < 12:
		fmt.Println("It's before noon")
	default:
		fmt.Println("It's after noon")
	}

	whatAmI := func(i interface{}) {
		switch t := i.(type) {
		case bool:
			fmt.Println("I'm a bool")
		case int:
			fmt.Println("I'm an int")
		case string:
			fmt.Println("I'm a string")
		default:
			fmt.Printf("Don't know type %T\n", t)
		}
	}
	whatAmI(true)
	whatAmI(1)
	whatAmI(c)
	var kk interface{} = "abc"
	s, ok := kk.(string)
	if ok {
		fmt.Println(s)
	} else {
		fmt.Println("not string")
	}
	//describe("nice to meet you ")
	//describe(123)

	ch := make(chan int)
	studyBreak(ch)
	time.Sleep(1 * time.Second)
	ch <- 1
	time.Sleep(2 * time.Second)
	ch <- 2
	time.Sleep(3 * time.Second)
	ch <- 3
}
func describe(i interface{}) {
	switch v := i.(type) {
	case int:
		fmt.Printf("Twice %v is %v\n", v, v*2)
	case string:
		fmt.Printf("%q is %v bytes long\n", v, len(v))
	default:
		fmt.Printf("I don't know about %T!\n", v)
	}
	var a = map[int]int{
		12: 12,
		40: 40,
	}
	fmt.Println(a)

	var ee = make(chan int)
	go func() {
		ee <- 42
	}()
	value := <-ee
	fmt.Println(value)
	fmt.Println(6 << 3)

	c1 := make(chan string)
	c2 := make(chan string)
	go func() {
		c1 <- "one"
	}()
	go func() {
		c2 <- "two"
	}()
	select {
	case msg1 := <-c1:
		fmt.Println("received:" + msg1)
	case msg2 := <-c2:
		fmt.Println("received:" + msg2)
	default:
		fmt.Println("no message received")
	}
}

func studyBreak(ch chan int) {
	for {
		select {
		case val := <-ch:
			fmt.Println(" Received from ch", ch)
			if val < 2 {
				fmt.Println("OK 继续执行")
			} else {
				return
			}
		default:
			fmt.Println(" nothing received default")
			time.Sleep(500 * time.Millisecond)
			return
		}
	}
}
